import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import compressPlugin from 'vite-plugin-compression'
import autoImport from 'unplugin-auto-import/vite'
import components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import basicSsl from '@vitejs/plugin-basic-ssl'
import vueJsx from '@vitejs/plugin-vue-jsx'

const target: string = 'http://192.168.0.186:5071' // 测试环境

// 需要注入的样式配置
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const isProduction = mode === 'production'

  return {
    // base: isProduction ? '/v3/trad' : '/',
    server: {
      https: false,
      proxy: {
        '^/api': {
          target: 'http://192.168.0.186:5071',
          changeOrigin: true,
          secure: false,
        },
      },
    },
    build: {
      minify: 'terser',
      terserOptions: {
        compress: {
          // 生产环境时移除console
          drop_console: true,
          drop_debugger: true,
        },
      },
    },
    css: {
      preprocessorOptions: {
        scss: { additionalData: '@use "@/assets/style/mixins.scss" as *;' },
      },
    },
    plugins: [
      vue(),
      vueJsx(),
      //访问https
      basicSsl(),
      // 自动导入 
      autoImport({
        imports: ['vue', 'vue-router', 'pinia'],
        dts: 'src/auto-import.d.ts',
        resolvers: [ElementPlusResolver({ importStyle: 'sass' })],
      }),
      // 组件自动导入
      components({
        dts: 'src/components.d.ts',
        resolvers: [ElementPlusResolver({ importStyle: 'sass' })],
      }),
      // 压缩
      compressPlugin({
        verbose: true, // 默认即可
        disable: false, // 开启压缩(不禁用)，默认即可
        deleteOriginFile: false, // 删除源文件
        threshold: 10240, // 压缩前最小文件大小
        algorithm: 'gzip', // 压缩算法
        ext: '.gz', // 文件类型
      }),
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
        '@common': fileURLToPath(new URL('./common-vue3', import.meta.url)),

      },
    },
  }
})

